package com.atguigu.springcloud;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

/**
 * @author Tu
 * @date 2023/6/11 18:09
 */
@SpringBootApplication
@EnableEurekaClient
//服务降级、熔断生效注解
@EnableCircuitBreaker
public class ProviderHystrix8003 {

    public static void main(String[] args) {
        SpringApplication.run(ProviderHystrix8003.class, args);
    }

    /**
     * Springcloud升级后的坑，添加此配置是为了服务监控配置
     * ServletRegistrationBean是因为springboot的默认路径不是"/hystrix.stream"
     * 所以需要添加配置更改，不然在配置页面会出现 Unable to connect to Command Metric Stream.
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlert() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

}
